package com.wmh.baseservice.operationaid.controller;

import com.wmh.baseservice.common.mybatisplus.structure.LambdaQueryWrapperX;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.common.utils.web.UserUtil;
import com.wmh.baseservice.operationaid.entity.FilterTemplate;
import com.wmh.baseservice.operationaid.enums.FilterTemplateType;
import com.wmh.baseservice.operationaid.pojo.vo.filterTemplate.FilterTemplateCommitReqVO;
import com.wmh.baseservice.operationaid.service.FilterTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author dongaolong
 * @createDate 2023-06-13
 */
@Validated
@RestController
@RequestMapping("operationAid/FilterTemplate")
@Api(tags = "筛选模板")
public class FilterTemplateController {

    @Resource
    private FilterTemplateService filterTemplateService;

    @Resource
    private UserUtil userUtil;

    @ApiOperation(value = "获取过滤模板列表")
    @GetMapping("/list")
    public String list(@NotNull(message = "未提交模板类型") FilterTemplateType type) {
        Long id = userUtil.getId();
        List<FilterTemplate> list = filterTemplateService.list(new LambdaQueryWrapperX<FilterTemplate>()
                .eq(FilterTemplate::getAdId, id)
                .eq(FilterTemplate::getType, type.getValue())
                .orderByDesc(FilterTemplate::getCreateTime));
        return ReturnBody.success(list);
    }

    @ApiOperation(value = "添加过滤模板")
    @PostMapping("/commit")
    public String commit(@Valid @RequestBody FilterTemplateCommitReqVO reqVO) {
        return ReturnBody.success(filterTemplateService.commitFilterTemplate(userUtil.getId(), reqVO));
    }

    @ApiOperation(value = "删除过滤模板")
    @PostMapping("/delete")
    public String delete(@NotNull(message = "未提交主键") Long id) {
        filterTemplateService.remove(new LambdaQueryWrapperX<FilterTemplate>()
                // 不是超级管理员只允许删除自己的模板
                .eq(!userUtil.isSuperAdmin(),FilterTemplate::getAdId, userUtil.getId())
                .eq(FilterTemplate::getId, id)
        );
        return ReturnBody.success();
    }
}